package com.zj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zj.common.Msg;
import com.zj.common.Result;
import com.zj.common.Utility;
import com.zj.entity.User;
import com.zj.entity.UserInfo;
import com.zj.mapper.UserInfoMapper;
import com.zj.mapper.UserMapper;
import com.zj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户服务impl
 *
 * @author zzj
 * @date 2023/02/22
 */
@Transactional
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * @param session
     * @param user    用户
     * @return {@code Result}
     */
    public Result userLogin(HttpSession session, User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("number",user.getNumber());
        List<User> userList = userMapper.selectList(userQueryWrapper);
        if(userList.size() == 0 || userList.size() > 1){ //是否查询到用户
            return Result.error(Msg.USER_IS_NULL);
        } else if (userList.get(0).getPassword().equals(user.getPassword())) { //密码正确
            //开始查询用户详细信息
            QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
            userInfoQueryWrapper.eq("user_id",userList.get(0).getId());
            List<UserInfo> userInfoList = userInfoMapper.selectList(userInfoQueryWrapper);
            //判断用户状态 1是正常 其余均不正常
            if (userInfoList.get(0).getUserStatus()!=1){
                return Result.error(Msg.USER_IS_ERROR);
            }else {
                //用户信息存入session
                session.setAttribute("user",userList.get(0));
                session.setAttribute("userInfo",userInfoList.get(0));
                return Result.success(userInfoList.get(0),Msg.Login_IS_SUCCESS);
            }
        }else { //密码不正确
            return Result.error(Msg.PASSWORD_IS_ERROR);
        }
    }

    /**
     * 用户注册
     *
     * @param user 用户
     * @return {@code Result}
     */
    @Override
    public Result userRegister(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number",user.getNumber());
        //查询用户是否已存在
        if (userMapper.selectOne(queryWrapper) == null){
            //user表添加
            userMapper.insert(user);
            //userInfo添加
            UserInfo userInfo = new UserInfo();
            userInfo.setNickname(Msg.USER_DEFAULT_NAME);
            userInfo.setAddress(Msg.USER_DEFAULT_ADD);
            userInfo.setUserId(user.getId()); //添加成功后会自动返回主键
            userInfo.setNumber(user.getNumber());
            userInfo.setCreateTime(Utility.getCurrentDate());
            userInfo.setRole("role");
            userInfoMapper.insert(userInfo);
            return Result.success(Msg.USER_CREATE_SUCCESS);
        }
        return Result.error(Msg.USER_CREATE_ERROR);
    }

    @Override
    public Result userIsOnline(HttpSession session) {
        UserInfo userInfo =(UserInfo) session.getAttribute("userInfo");
        if (userInfo!=null) {
            return  new Result<>().add("code",1).add("msg",Msg.USER_IS_ONLINE);
        }else {
            return new Result<>().add("code",0).add("msg",Msg.USER_NOT_ONLINE);
        }
    }
}
